#include <iostream>
#include <fstream>
#include "inp.h"
#include "declaration.h"
#include "flow.h"
// using namespace inp;
using namespace std;

/**
 * @brief 回显用户的输出
 * 
 */
void confirm() noexcept
{
    // using namespace inp;
    cout << "p01 = " << inp::p01 << "\n"
         << "t01 = " << inp::t01 << "\n"
         << "p2 = " << inp::p2 << "\n"
         << "gamma = " << inp::gamma << "\n"
         << "cpgas = " << inp::cpgas << "\n"
         << "cfl = " << inp::cfl << "\n"
         << "epsirs = " << inp::epsirs << "\n";

    if (inp::kdissip == "c" || inp::kdissip == "C")
        cout << "vis2 = " << inp::vis2 << "\n"
             << "vis4 = " << inp::vis4 << "\n";
    else if (inp::kdissip == "r" || inp::kdissip == "R")
        cout << "iorder = " << inp::iorder << "\n"
             << "limfac = " << inp::limfac << "\n"
             << "epsentr = " << inp::epsentr << "\n";
}

void OutPut(const flow& result, const Grid& grid) noexcept
{
    // using namespace inp;
    ofstream solution{inp::fnplot, ios_base::out};
    if(!solution) 
    {
        cerr << "open solution file err\n";
        exit(EXIT_FAILURE);
    }
    solution << "TITLE = \" visualization of the flow\" \n" 
        << "VARIABLES = \"x\", \"area\", \"rho\", \"u\", \"p\", \"T\", \"Mach\", \"mass flow\" \n";
    
    double rho, u, temp, c, mach;
    const vec1d<double>& p = result.p;
    const vec2d<double>& cv = result.cv;
    const vec1d<double>& area = grid.area;
    const vec1d<double>& x = grid.x;
    const size_t ib2 = grid.ib2;

    for(size_t i = 1; i < ib2; ++i)
    {
        rho = cv(i, 0) / area(i);
        u = cv(i, 1) / cv(i, 0);
        temp = p(i)/(rho*inp::rgas);
        c = sqrt(inp::gamma * p(i)/ rho);
        mach = u/c;

        solution << scientific << setprecision(9)
        << x(i) << "\t" << area(i) << "\t" 
        << rho << "\t" << u << "\t"
        << p(i) << "\t" << temp << "\t"
        << mach << "\t" << cv(i, 1) << "\n";
    }
    solution.close();
}